<%-- 
    Document   : Admin
    Created on : 09-feb-2012, 15:17:15
    Author     : Alvaro
--%>

<%@page import="java.io.File"%>
<%@page import="classes.graph"%>
<%@page import="java.util.List"%>
<%@page import="classes.AdminDB"%>
<%@page import="classes.order"%>
<%@page import="classes.user"%>
<%@page import="java.sql.*"%>
<%@page import="classes.product"%>
<%@page import="java.util.ArrayList"%>
<%ArrayList<product> productos = (ArrayList<product>) session.getAttribute("busquedaProductos");%>
<%ArrayList<user> users = (ArrayList<user>) session.getAttribute("listaDeusuarios");%>
<%ArrayList<order> orders = (ArrayList<order>) session.getAttribute("listaDepedidos");%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="Admin.MailSender"%><!-- LLAMAMOS A LA CLASE QUE ENVIA MENSAJES -->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
        <link rel="stylesheet" type="text/css" href="../css/admin.css" />


        <title>ADMIN</title>
        <script>
            
            function seguroU(){
                with(document.deleteAllUsers){
                    if(confirm("Delete? Sure?")){
                        submit();

                    }
                    
                    else{

                    }
                    
                }
            }
            
            function seguroO(){
                with(document.deleteAllOrders){
                    if(confirm("Delete? Sure?")){
                        submit();

                    }
                    
                    else{

                    }
                    
                }
            }
            
            function seguroP(){
                with(document.deleteAllProducts){
                    if(confirm("Delete? Sure?")){
                        submit();

                    }
                    
                    else{

                    }
                    
                }
            }
            
        </script>







    </head>
    <br><br>
    <body>

        <%
            String cookieName = "admin";
            Cookie cookies[] = request.getCookies();

            Cookie admin = null;
            if (cookies != null) {
                for (int i = 0; i < cookies.length; i++) {
                    if (cookies[i].getName().equals(cookieName)) {
                        admin = cookies[i];
                        break;
                    }
                }
            }
            if (admin == null) {
        %><meta http-equiv="Refresh" content="0;url=../index.jsp"><%                    } else {
                if (admin.getValue().equals("admin")) {
                    System.out.println("Estamos dentro de administracion");
                }
            }
        %>

        <div class="container"><!-- header -->
            <h1 align="center">Admin console</h1>

        </div><!-- end header -->

        <div class="container">
            <form name="rstSession" method="get" action="/ShopOnline/cleanSessionServlet">
                <input type="submit" name="rst" value="Clean all searches">
            </form>
            <form name="loadAllProd" method="get" action="/ShopOnline/loadAllProductsServlet">
                <input type="submit" name="load" value="Load all products">
            </form>
        </div>

        <div class="container"><!-- contenido -->

            <h2 class="titulo">Products</h2>
            <h3><u>SEARCH</u> </h3>
            <form name="search" method="get" action="/ShopOnline/searchProducts">
                <p>
                    <label for="search"></label>
                    <input name="suggest1" type="text" class="input" id="suggest1" placeholder="Search..." autocomplete="on">
                    <input type="submit" name="go" id="go" value="GO!">
                <p>
                    <label for="by">by</label>
                    <select name="by" id="by">
                        <option value="team">team</option>
                        <option value="name">name</option>
                        <option value="description">description</option>
                        <option value="price">price</option>
                    </select>
                </p>
            </form></p>



        <%
            if (productos == null) {%>

        <%} else {
            for (product cl : productos) {%>


        <table>
            <tr>
                <td width="59"><label><strong>Id: </strong> <%=cl.getId()%></label></td>
                <td width="59"><label><strong>Team:</strong> <%=cl.getTeam()%></label></td>
                <td width="62"><label><strong>Name: </strong><%=cl.getName()%></label></td>
                <td width="103"><label><strong>Description: </strong><%=cl.getDescription()%></label></td>
                <td width="42"><label><strong>Stock:</strong> <%=cl.getStock()%></label></td>
                <td width="258"><img src="../<%=cl.getDir()%>" alt="photo" width="250" height="150" class="borderImages"></td>
                <td width="80"><label class="tableComments"><%=cl.getPrice()%> € </label></td>
                <td width="15"> 
                    <form action="/ShopOnline/deleteProductFromAdmin" name="deleteOrder" method="post">
                        <input type="hidden" name="id" value="<%=cl.getId()%>">
                        <input type="image" name="delete" src="../images/remove.png" />
                    </form>
                </td>

            </tr>
        </table>


        <%}
            }%>



        <p>&nbsp;</p>
        <hr>
        <h3><u>MODIFY</u></h3>
        <form action="/ShopOnline/modifyProduct" method="post" name="add">
            <table width="73%" border="0">
                <tr>
                    <td width="21%">ID</td>
                    <td width="79%"><label for="idMod"></label>
                        <input type="text" name="idMod" id="idMod" placeholder="Product ID"> 
                        Id of the product to change</td>
                </tr>
                <tr>
                    <td width="21%">Team</td>
                    <td width="79%" align="left">
                        <label for="teamMod"></label>
                        <label for="teamMod"></label>
                        <select name="teamMod" id="teamMod">
                            <option value="Ferrari">Ferrari</option>
                            <option value="Williams">Williams</option>
                            <option value="Force India" selected>Force India</option>
                            <option value="Toro Rosso">Toro Rosso</option>
                            <option value="HRT">HRT</option>
                            <option value="Redbull">Redbull</option>
                            <option value="Mercedes">Mercedes</option>
                            <option value="Lotus">Lotus</option>
                            <option value="Renault">Renault</option>
                            <option value="McClaren">McClaren</option>
                            <option value="Virgin">Virgin</option>
                            <option value="Sauber">Sauber</option>
                            <option value="Marussia">Marussia</option>
                            <option value="Caterham">Caterham</option>
                        </select>

                    </td>
                </tr>
                <tr>
                    <td>Name</td>
                    <td><input type="text" name="nameMod" id="nameMod" placeholder="New name"></td>
                </tr>
                <tr>
                    <td>Price</td>
                    <td><input name="priceMod" type="text" id="priceMod" size="6" maxlength="6" placeholder="Price">
                        €</td>
                </tr>
                <tr>
                    <td valign="top">Description</td>
                    <td><textarea name="descriptionMod" rows="5" id="descriptionMod" placeholder="New Description"></textarea></td>
                </tr>
                <tr>
                    <td>Stock</td>
                    <td><input name="stockMod" type="text" id="stockMod" size="6" maxlength="4" placeholder="Stock"></td>
                </tr>
                <tr>
                    <td>Photo dir.</td>
                    <td>
                        Images on the server:
                        <select name="dirMod" id="dirMod">
                            <%
                                //Modificar direccion!!
                                File dir = new File("/Users/Alvaro/Desktop/ShopOnline/ShopOnline/web/images/carimages/");
                                String[] ficheros = dir.list();
                                if (ficheros == null) {
                                    System.out.println("No hay ficheros en /Users/Alvaro/Desktop/ShopOnline/ShopOnline/web/images/carimages/");
                                } else {
                                    System.out.println(request.getContextPath());
                                    for (int x = 0; x < ficheros.length; x++) {
                                        System.out.println(ficheros[x]);%>

                            List of images:

                            <option value="images/carimages/<%=ficheros[x]%>"><%=ficheros[x]%></option>
                            <%
                                    }
                                }

                            %>
                        </select></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><label for="hrefMod"></label></td>
                </tr>
            </table>
            <p>
                <input type="submit" name="Mod" id="Mod" value="Modify">
            </p>
        </form>
        <hr>
        <h3><u>ADD</u></h3>
        <form action="/ShopOnline/addproduct" method="post" name="add">
            <table width="93%" border="0">
                <tr>
                    <td width="21%">Team</td>
                    <td width="79%"><label for="team"></label>
                        <select name="team" id="team">
                            <option value="Ferrari">Ferrari</option>
                            <option value="Williams">Williams</option>
                            <option value="Force India">Force India</option>
                            <option value="Toro Rosso">Toro Rosso</option>
                            <option value="HRT">HRT</option>
                            <option value="Redbull">Redbull</option>
                            <option value="Mercedes">Mercedes</option>
                            <option value="Lotus">Lotus</option>
                            <option value="Renault">Renault</option>
                            <option value="McClaren">McClaren</option>
                            <option value="Virgin">Virgin</option>
                            <option value="Sauber">Sauber</option>
                            <option value="Marussia">Marussia</option>
                            <option value="Caterham">Caterham</option>
                        </select></td>
                </tr>
                <tr>
                    <td>Name</td>
                    <td><input type="text" name="name" id="name" placeholder="Name"></td>
                </tr>
                <tr>
                    <td>Price</td>
                    <td><input name="price" type="text" id="price" size="6" maxlength="6" placeholder="Price">
                        €</td>
                </tr>
                <tr>
                    <td valign="top">Description</td>
                    <td><textarea name="description" rows="5" id="description" placeholder="Description of the product"></textarea></td>
                </tr>
                <tr>
                    <td>Stock</td>
                    <td><input name="stock" type="text" id="stock" size="6" maxlength="4" placeholder="Stock"></td>
                </tr>
                <tr>
                    <td>Photo dir. (optional)</td>
                    <td>Images on the server:
                        <select name="dir" id="dir">
                            <%

                                if (ficheros == null) {
                                    System.out.println("No hay ficheros en /Users/Alvaro/Desktop/ShopOnline/ShopOnline/web/images/carimages/");
                                } else {
                                    System.out.println(request.getContextPath());
                                    for (int x = 0; x < ficheros.length; x++) {
                                        System.out.println(ficheros[x]);%>

                            List of images: 
                            <option value="images/carimages/<%=ficheros[x]%>"><%=ficheros[x]%></option>
                            <%
                                    }
                                }

                            %>
                        </select></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td><input type="submit" name="add2" id="add" value="Add"></td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
            </table>
            <p>&nbsp;</p>
        </form>
        <form action="/ShopOnline/UploadServlet" method="post" enctype="multipart/form-data" name="form1">
            <table width="73%" border="0">
                <tr>
                    <td width="21%">Upload photo</td>
                    <td width="79%"><label for="file"></label>
                        <input type="file" name="file" id="file"></td>
                </tr>
            </table>
            <p>
                <input type="submit" name="uploadphoto" id="uploadphoto" value="Add">
            </p>
        </form>

        <hr>
        <h3><u>DELETE</u></h3>
        <form name="delete" method="post" action="/ShopOnline/deleteproduct">
            <table width="73%" border="0">
                <tr>
                    <td width="21%">Team</td>
                    <td width="79%">
                        <label for="teamdelete"></label>
                        <select name="teamdelete" id="teamdelete">
                            <option value="Ferrari">Ferrari</option>
                            <option value="Williams">Williams</option>
                            <option value="Force India">Force India</option>
                            <option value="Toro Rosso">Toro Rosso</option>
                            <option value="HRT">HRT</option>
                            <option value="Redbull">Redbull</option>
                            <option value="Mercedes">Mercedes</option>
                            <option value="Lotus">Lotus</option>
                            <option value="Renault">Renault</option>
                            <option value="McClaren">McClaren</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Product Name</td>
                    <td><input type="text" name="namedelete" id="namedelete"></td>
                </tr>
            </table>
            <p>
                <input type="submit" name="delete" id="delete" value="Delete">
            </p>
        </form>
        <p>&nbsp; </p>
        <form name="deleteAllProducts" method="post" action="/ShopOnline/deleteAllProduct">
            <input type="button" name="deleteallproduct" id="deleteallproduct" value="Delete all products..." onclick="seguroP()">
        </form>

    </div>
    <br><br>
    <div class="container"><!-- contenido -->



        <h2 class="titulo">Users      </h2>
        <form name="view-order" method="get" action="/ShopOnline/listUsers">
            <p>
                <label for="view-order2"></label>
                <input name="viewOrder2" type="text" class="input" id="viewOrder2" placeholder="Search..." autocomplete="on">
                <input type="submit" name="go3" id="go3" value="GO!">
                <input type="hidden" name="stats" value="admin">
                <a href="Admin.jsp"><img src="../images/back.png" width="20" height="20" alt="refresh"></a></p>
            <p>
                <label for="searchOrder2">by</label>
                <select name="searchOrder2" id="searchOrder2">
                    <option value="email">Email</option>
                    <option value="name">Name</option>
                    <option value="country">Country</option>
                    <option value="city">City</option>
                </select>
            </p>
        </form>



        <%
            if (users == null) {%>

        <%} else {
            for (user u : users) {%>


        <table>
            <tr>
                <td width="140"><img src="../<%=u.getDir()%>" alt="photo" width="120" height="150" class="borderImages"></td>
                <td width="100"><label><strong>Name</strong> <%=u.getName()%></label></td>
                <td width="150"><label><strong>Lastname </strong><%=u.getSecondname()%></label></td>
                <td width="250"><label><strong>Email </strong><%=u.getEmail()%></label></td>
                <td width="120"><label><strong>Country</strong> <%=u.getCountry()%></label><img src="../images/Flags/<%=u.getCountry()%>.gif" width="20" height="20"></td>
                <td width="100"><label><strong>City </strong><%=u.getCity()%></label></td>
                <% String deleting = u.getEmail();
                    System.out.println("Email que mandamos desde la jsp para borrar: " + deleting);
                    System.out.println("URL de la foto: " + u.getDir());
                %>
                <td width="15"> 
                    <form action="/ShopOnline/deleteUserFromAdmin" name="deleteUser" method="post">
                        <input type="hidden" name="email" value="<%=deleting%>">
                        <input type="image" name="delete" src="../images/remove.png" />
                    </form>
                </td>

            </tr>
        </table>


        <%}
            }%>









        <p></p>
        <form name="deleteAllUsers" method="post" action="/ShopOnline/deleteAllUser">
            <input type="button" name="deleteallusers" id="deleteallusers" value="Delete all users..." onclick="seguroU()">
        </form>

    </div>
    <br><br>
    <div class="container"><!-- contenido -->


        <h2 class="titulo">Orders </h2>
        <h3> VIEW         </h3>
        <form name="view-order" method="get" action="/ShopOnline/listOrders">
            <p>
                <label for="view-order"></label>
                <input type="hidden" name="orderinfo" value="admin">
                <input type="hidden" name="emailSearch" value="x">

                <input name="viewOrder" type="text" class="input" id="viewOrder" placeholder="Search..." autocomplete="on">
                <input type="submit" name="go2" id="go2" value="GO!">
                <a href="Admin.jsp"><img src="../images/back.png" width="20" height="20" alt="refresh"></a></p>
            <p>
                <label for="searchOrder">by</label>
                <select name="searchOrder" id="searchOrder">
                    <option value="customeremail">Email</option>
                    <option value="customername">Name</option>
                    <option value="product">Product</option>
                    <option value="price">Price</option>
                    <option value="customercountry">Country</option>
                    <option value="customercity">City</option>
                    <option value="paymentmethod">Payment method</option>
                </select>
            </p>
        </form>

        <%
            if (orders == null) {%>

        <%} else {
            for (order o : orders) {%>

        <table>
            <tr><hr color="#CCC" width="700px"></tr>
            <tr>

                <td><label><strong>Id</strong>: <%=o.getId()%></label> Destination: <img src="../images/Flags/<%=o.getCustomercountry()%>.gif" width="20" height="20"></td>
                <td><label><strong>Product</strong>: <%=o.getProduct()%></label></td>
                <td><strong>Qty</strong>: <%=o.getQty()%></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td><label><strong>Payment method</strong>: <%=o.getPaymentmethod()%></label></td>

            </tr>
            <tr>
                <td>&nbsp;</td>
                <td><strong>Price</strong>: <%=o.getPrice()%></td>
                <td><strong>Date</strong>: <%=o.getPaymentdate()%></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr> 
            <tr>
                <td></td>
                <td><strong>Email</strong>: <%=o.getCustomeremail()%></td>
                <td><strong>Name</strong>: <%=o.getCustomername()%> <%=o.getCustomersecondname()%></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td> 
                <td>
                    <form action="/ShopOnline/deleteOrderFromAdmin" name="deleteOrder" method="post">
                        <input type="hidden" name="id" value="<%=o.getId()%>">
                        <input type="image" name="delete" src="../images/remove.png" />
                    </form>
                </td>
            <tr>
                <td></td>
            </tr>
            <tr>
                <td></td>
            </tr> 
        </table>

        <%}
            }%>   



        <form name="deleteAllOrders" method="post" action="/ShopOnline/deleteAllOrder">
            <input type="hidden" name="deleteAllOrders" value="1">

            <input type="button" name="deleteallorders" id="deleteallorders" value="Delete all orders..." onclick="seguroO()">
        </form>
    </div>
    <br><br>
    <div class="container"><!-- contenido -->


        <h2 class="titulo">Statistics</h2>

        <a href="statistics.jsp"><img src="../images/stats.jpg" width="300" height="225" alt="stats">View statistics</a>

    </div>
    <br><br>
    <div class="container"><!-- contenido -->


        <h2 class="titulo">Send Email</h2>

        <!-- ENVIO DEL EMAIL -->

        <form action="/ShopOnline/thisServletSendstheEmail" name="send" id="send" >
            <p>&nbsp;</p>
            <table width="60%" border="0">
                <tr>
                    <td>From:</td>
                    <td><input name="from" type="text" id="from" value="alvarof1store@gmail.com" size="30"></td>
                </tr>
                <tr>
                    <td>To:</td>
                    <td><input type="text" name="to" id="to" placeholder="to..."></td>
                </tr>
                <tr>
                    <td>Subjet:</td>
                    <td><input type="text" name="subjet" id="subjet" placeholder="Subject"></td>
                </tr>
                <tr>
                    <td valign="top">Message:</td>
                    <td><textarea name="msg" cols="60" rows="8" id="msg"></textarea></td>
                </tr>
            </table>
            <input type="submit" name="sendemail" id="sendemail" value="Send">
            <p>&nbsp;</p>
        </form>


        <p>
        </p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
    </div><!-- end contenido -->

    <div class="container"><!-- footer -->
        <div align="center"><a href="../index.jsp">Store </a></div>
    </div><!-- end footer-->
    <br><br><br>
</body>
</html>
